package com.itmayiedu;

import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 订单服务启动类
 * @EnableEurekaClient [将该服务注册到eureka注册中心]
 * @EnableZuulProxy [服务开启Zuul网关反向代理功能]
 * @EnableSwagger2Doc [服务开启swagger接口管理平台，无需编写swagger配置文件类]
 * @author: liucancan
 * @create: 2018-11-03 16:29
 **/
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
public class AppZuulGateWay {
    public static void main(String[] args) {
        SpringApplication.run(AppZuulGateWay.class, args);
    }

    /**
     * zuul配置能够使用config实现实时更新，手动调用此接口可以让本服务从远程配置仓库中更新zuul配置信息
     */
    @RefreshScope
    @ConfigurationProperties("zuul")
    public ZuulProperties zuulProperties() {
        return new ZuulProperties();
    }

    /**
     * 编写网关项目整合所有微服务swagger组件
     *
     * @param
     * @author liucancan
     * @date 17:45 2018/11/27
     * @return
     */
    @Component
    @Primary
    class DocumentationConfig implements SwaggerResourcesProvider {
        @Override
        public List<SwaggerResource> get() {
            List resources = new ArrayList<>();
            /**
             * swaggerResource("自定义服务swagger名称","/网关项目zuul配置中的对应服务path值/v2/api-docs","该服务swagger接口版本号"
             */
            resources.add(swaggerResource("app-itmayiedu-member", "/api-member/v2/api-docs", "2.0"));
            resources.add(swaggerResource("app-itmayiedu-order", "/api-order/v2/api-docs", "2.0"));
            return resources;
        }

        private SwaggerResource swaggerResource(String name, String location, String version) {
            SwaggerResource swaggerResource = new SwaggerResource();
            swaggerResource.setName(name);
            swaggerResource.setLocation(location);
            swaggerResource.setSwaggerVersion(version);
            return swaggerResource;
        }
    }
}